const { 
    getType,
    updateType,
    createType,
    destroyType
} = require('../service/type'),
     { updateToQiniu, returnInfo, fileUploadLegel } = require('../lib/utils'),
     { nanoid } = require('nanoid'),
     { IMAGE_UPLOAD,API } = require('../config/err_config'),
     { qiniu } = require('../config/config'),
     path = require('path'),
     { typeisExit } = require('../service/essay')
class Type{
    async getTypeC(ctx){
        try {
            const { id } = ctx.request.body;
            let returnMess = await getType(id);
            ctx.body = returnInfo(API.GET_TYPE_SUCCESS,returnMess);
        } catch (error) {
            console.log(error);
            ctx.body = returnInfo(API.GET_TYPE_ERROR);
        }
    }
    async createTypeC(ctx){
        let  typeimg = '';
        let isLegel = fileUploadLegel(ctx);
        if(isLegel){
            const files = Object.entries(ctx.request.files),
            {typename} = ctx.request.body;
     
          let extname = path.extname(files[0][1].name),
          filePath = files[0][1].path,
          fileName = `typephoto${nanoid()}${extname}`;
              try {
                  updateToQiniu(filePath,fileName);
                  typeimg = `${qiniu.bucket.blogsimg.domain}/${fileName}`;
              } catch (error) {
                  console.log(error);
                  ctx.body = IMAGE_UPLOAD.UPLOAD_ERROR;
              }
              try {
                let returnMess = await createType(typename,typeimg)
                ctx.body = returnMess ? returnInfo(API.CREATE_TYPE_SUCCESS)
                                      : returnInfo(API.CREATE_TYPE_ERROR)
             } catch (error) {
                 console.log(error);
                 ctx.body = returnInfo(API.CREATE_TYPE_ERROR);
             }
        }else{
            ctx.body = returnInfo(IMAGE_UPLOAD.UPLOAD_ERROR)
            return;
        }      
       
    }
    async destroyTypeC(ctx){
        let {id} = ctx.request.body,
            isExit = true;
        try {
            let count = await typeisExit(id);
            isExit =  count[0].dataValues.count > 0 ? true : false; 
        } catch (error) {
            console.log(error);
            ctx.body = returnInfo(API.SELECT_TYPEID_ISEXIT_ERROR)
        }
        if(!isExit){
            try {
                let retrunMess = await destroyType(id);
                ctx.body = retrunMess ? returnInfo(API.DESTROY_TYPE_SUCESS)
                                      : returnInfo(API.DESTROY_TYPE_ERROR)
            } catch (error) {
                console.log(error);
                ctx.body = returnInfo(API.DESTROY_TYPE_ERROR);
            }
          
        }else{
            ctx.body = returnInfo(API.DESTROY_TYPE_ERROR_ISEXIT);
        }
    }
    async updateTypeC(ctx){
        const files = ctx.request.files,
              {typename} = ctx.request.body,
              typeimg = null;
        if(files){
            let extname = path.extname(files.name),
                filePath = files.path,
                fileName = `typephoto${nanoid()}${extname}`;
                try {
                    updateToQIniu(filePath,fileName);
                    typeimg = `${qiniu.bucket.blogsimg.domain}/${fileName}`;
                } catch (error) {
                    console.log(error);
                    ctx.body = IMAGE_UPLOAD.UPLOAD_ERROR;
                }
                
        }    
        try {
           let returnMess = await updateType(typename,typeimg)
           ctx.body = returnMess ? returnInfo(API.UPDATE_TYPE_SUCCESS)
                                 : returnInfo(API.UPDATE_TYPE_ERROR)
        } catch (error) {
            console.log(error);
            ctx.body = returnInfo(API.UPDATE_TYPE_ERROR);
        }
    }
}     
module.exports = new Type();